const CryptoJS = require("crypto-js")
var akb33 = "32223";
var akb34 = "N4EDAQpO2ejqgCoX";
var akb35 = "=qoKNLgdAjJbU8zx";
var akb36 = "mAkJqt8coXQ96zML";
var akb48 = "t4ABRmeN"

function md5(input) {
    return CryptoJS.MD5(input).toString()
}


/*
* "GETCITYAQIRANK"
*{
    "order": "desc"
}
*
* */
function ObjectSort(obj) {
    var newObject = {};
    Object.keys(obj).sort().map(function (key) {
        newObject[key] = obj[key]
    });
    return newObject
}

function Base64() {
    // 编码表
    this._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    // 编码方法
    this.encode = function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = this._utf8_encode(input);

        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
                this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
                this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
        }

        return output;
    };

    // UTF-8 编码方法
    this._utf8_encode = function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }

        return utftext;
    };
}

var AES = {
    encrypt: function (text) {
        var secretkey = md5(akb34).substr(16, 16);
        var secretiv = md5(akb35).substr(0, 16);
        secretkey = CryptoJS.enc.Utf8.parse(secretkey);
        secretiv = CryptoJS.enc.Utf8.parse(secretiv);
        var result = CryptoJS.AES.encrypt(text, secretkey, {
            iv: secretiv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return result.toString()
    },
    decrypt: function (text) {
        var secretkey = (CryptoJS.MD5(akb34).toString()).substr(16, 16);
        var secretiv = (CryptoJS.MD5(akb35).toString()).substr(0, 16);
        secretkey = CryptoJS.enc.Utf8.parse(secretkey);
        secretiv = CryptoJS.enc.Utf8.parse(secretiv);
        var result = CryptoJS.AES.decrypt(text, secretkey, {
            iv: secretiv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return result.toString(CryptoJS.enc.Utf8)
    },
};
var DES = {
    encrypt: function (text) {
        var secretkey = (CryptoJS.MD5(akb36).toString()).substr(0, 16);
        var secretiv = (CryptoJS.MD5(akb48).toString()).substr(24, 8);
        secretkey = CryptoJS.enc.Utf8.parse(secretkey);
        secretiv = CryptoJS.enc.Utf8.parse(secretiv);
        var result = CryptoJS.DES.encrypt(text, secretkey, {
            iv: secretiv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return result.toString()
    },
    decrypt: function (text) {
        var secretkey = (CryptoJS.MD5(akb36).toString()).substr(0, 16);
        var secretiv = (CryptoJS.MD5(akb48).toString()).substr(24, 8);
        secretkey = CryptoJS.enc.Utf8.parse(secretkey);
        secretiv = CryptoJS.enc.Utf8.parse(secretiv);
        var result = CryptoJS.DES.decrypt(text, secretkey, {
            iv: secretiv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return result.toString(CryptoJS.enc.Utf8)
    }
};

function MyEncode(str) {
    var arr = akb33.split('')
    var b = new Base64;
    arr.forEach(times => {
            switch (times) {
                case "1":
                    str = AES.encrypt(str)
                    break;
                case "2":
                    str = DES.encrypt(str)
                    break;
                case "3":
                    str = b.encode(str)
                    break;
            }
        }
    )
    return str;
}

function zxWVS(method, obj) {
    var appId = '4f0e3a273d547ce6b7147bfa7ceb4b6e';
    var timestamp = new Date().getTime();
    var need = {
        appId: appId,
        method: method,
        timestamp: timestamp,
        clienttype: 'WEB',
        object: obj,
        secret: md5(appId + method + timestamp + 'WEB' + JSON.stringify(ObjectSort(obj)))
    };
    return MyEncode(JSON.stringify(need))
}
function result_param(){
    return _0x4a879b = zxWVS("GETCITYAQIRANK", {"order": "desc"})
}

